﻿/*
 * Mr.Chau
 * 28.05.2013
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NangLuongTieuDiemDAO.DAO;
using NangLuongTieuDiemDTO.DTO;
using NangLuongTieuDiemBUS.SDK;
using System.Data;
using System.Text.RegularExpressions;

namespace NangLuongTieuDiemBUS.BUS
{
    public class SanPhamBUS
    {


        #region "[lấy loại sản phẩm chính]"
        /// <summary>
        /// lấy loại sản phẩm chính
        /// </summary>
        private enum ProductType
        {
            Pin,
            BinhAcQuy,
            BoDieuKienSac,
            ThietBiDien,
            KichDien
        }

        /// <summary>
        /// lấy loại sản phẩm chính
        /// </summary>
        /// <param name="iType"></param>
        /// <returns></returns>
        private ProductType GetType(int iType)
        {
            ProductType objResult = ProductType.Pin;
            try
            {
                switch (iType)
                {
                    case 1:
                        return ProductType.Pin;
                    case 2:
                        return ProductType.BinhAcQuy;
                    case 3:
                        return ProductType.BoDieuKienSac;
                    case 4:
                        return ProductType.ThietBiDien;
                    default:
                        return ProductType.KichDien;
                }

            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("GetType", ex);
            }
            return objResult;
        }
        #endregion

        #region "[Thêm Pin năng lượng mặt trời]"
        /// <summary>
        /// Thêm Pin năng lượng mặt trời
        /// </summary>
        /// <param name="spDto"></param>
        /// <returns></returns>
        public int Insert(SanPhamDTO spDto)
        {
            int IDResult = -1;
            try
            {
                IDResult = SanPhamDAO.Insert(spDto);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("Insert", ex);
            }
            return IDResult;

        }
        #endregion

        #region "[Delete]"
        public bool Delete(int ID)
        {
            // Kiem tra Business Rule



            // Xoa
            bool IDResult = false;
            try
            {
                IDResult = SanPhamDAO.Delete(ID);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("Delete", ex);
            }
            return IDResult;
        }
        #endregion

        #region "[Update]"
        public bool Update(SanPhamDTO spDto)
        {
            // Kiem tra Business Rule



            // Sua
            bool IDResult = false;
            try
            {
                IDResult = SanPhamDAO.Update(spDto);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("Update", ex);
            }
            return IDResult;
        }
        #endregion

        #region "[Get all Pin]"
        public List<SanPhamDTO> GetAllPin()
        {
            List<SanPhamDTO> dsspDto = new List<SanPhamDTO>();
            try
            {
                dsspDto = SanPhamDAO.GetAll();
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("GetAllPin", ex);
            }
            return dsspDto;
        }
        #endregion

        #region "[SelectByPrimaryKey]"
        public SanPhamDTO SelectByPrimaryKey(int ID)
        {
            SanPhamDTO spDto = new SanPhamDTO();
            try
            {
                spDto = SanPhamDAO.SelectByPrimaryKey(ID);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("SelectByPrimaryKey", ex);
            }
            return spDto;
        }
        #endregion

        #region "[Lấy danh sách các tấm Pin ứng với công suất]"
        public List<SanPhamDTO> GetListPinByPower(double _PowerValue, int _PinType)
        {
            List<SanPhamDTO> listPinResult = new List<SanPhamDTO>();
            try
            {

                //B1: lấy tấm pin có cung công suất
                SanPhamDTO PinCungCongSuat = null;
                try
                {
                    PinCungCongSuat = GetAllPin().Where(p => p.CongSuat == _PowerValue && p.LoaiSanPhamID == _PinType).First();
                }
                catch { }

                if (PinCungCongSuat != null)
                {
                    listPinResult.Add(PinCungCongSuat);
                    return listPinResult;
                }
                else
                {
                    //b2: Lấy danh sách các tấm Pin có công suất lớn hơn công suất các thiết bị.
                    List<SanPhamDTO> listPinMax = new List<SanPhamDTO>();
                    try
                    {
                        listPinMax = GetAllPin().Where(p => p.CongSuat > _PowerValue && p.LoaiSanPhamID == _PinType).OrderBy(p => p.CongSuat).ToList();
                    }
                    catch { }
                    //B3: Lấy danh sách các tấm Pin có công suất nhỏ hơn công suất các thiết bị.
                    List<SanPhamDTO> listPinMin = new List<SanPhamDTO>();
                    try
                    {
                        listPinMin = GetAllPin().Where(p => p.CongSuat < _PowerValue && p.LoaiSanPhamID == _PinType && p.CongSuat > 0).OrderByDescending(p => p.CongSuat).ToList();
                    }
                    catch { }
                    //lấy danh sách các tấm pin đáp ứng nổi công suất.
                    if (listPinMin.Count > 0)
                    {
                        int irun = 0;
                        //công suất thiết bị
                        double congSuatThietBi = _PowerValue;

                        while (irun < listPinMin.Count)
                        {
                            irun++;
                            SanPhamDTO item = null;
                            try { item = listPinMin.Where(p => p.CongSuat == congSuatThietBi).First(); }
                            catch { }
                            if (item != null)
                            {
                                listPinResult.Add(item);
                                goto Lap;
                            }
                            else
                            {
                                item = null;
                                try { item = listPinMin.Where(p => p.CongSuat < congSuatThietBi).First(); }
                                catch { }
                                if (item != null)
                                {
                                    double temp = congSuatThietBi - item.CongSuat;
                                    if (temp >= 0)
                                    {
                                        congSuatThietBi -= item.CongSuat;
                                        listPinResult.Add(item);
                                    }
                                }
                            }
                        }
                    }

                Lap:
                    double moneyPinMax = 0;
                    if (listPinResult.Count > 0)
                    {
                        foreach (SanPhamDTO item in listPinResult)
                        {
                            moneyPinMax += item.Gia;
                        }
                    }
                    if (listPinMax.Count > 0)
                    {
                        if (moneyPinMax <= listPinMax[0].Gia)
                        {
                            return listPinResult;
                        }
                        else
                        {
                            listPinResult = new List<SanPhamDTO>();
                            listPinResult.Add(listPinMax[0]);
                        }
                    }

                }
                #region "rem here"
                /*
                List<SanPhamDTO> listPin = GetAllPin().OrderByDescending(p => p.CongSuat).ToList().Where(p => p.LoaiSanPhamID == _PinType).ToList();
                if (listPin.Count > 0)
                {
                    
                    //tổng công suất thiết bị
                    double congsuatThietBi = _PowerValue;
                    int irun = 0;
                    while (irun < listPin.Count)
                    {
                        irun++;
                        //lấy tấm pin có công suất cao hơn công suất của thiết bị.
                        SanPhamDTO PinMax = GetAllPin().Where(p => p.CongSuat >= congsuatThietBi).OrderBy(p => p.CongSuat).ToList().First();
                        if (PinMax != null)
                        {
                            congsuatThietBi -= PinMax.CongSuat;
                            listPinResult.Add(PinMax);
                        }
                        else
                        {
                            SanPhamDTO PinMin = GetAllPin().Where(p => p.CongSuat < congsuatThietBi && p.CongSuat > 0).
                                OrderByDescending(p => p.CongSuat).ToList().First();
                            if (PinMin != null)
                            {
                                congsuatThietBi -= PinMin.CongSuat;
                                listPinResult.Add(PinMin);
                            }
                        }
                        if (congsuatThietBi < 0)
                            return listPinResult;
                    }

                    //foreach (SanPhamDTO item in listPin)
                    //{
                    //    if (congsuatThietBi > 0 && item.CongSuat>0)
                    //    {
                    //        double congsuatTemp = (congsuatThietBi - item.CongSuat);
                    //        if (congsuatTemp >= 0)
                    //        {
                    //            congsuatThietBi -= item.CongSuat;
                    //            listPinResult.Add(item);
                    //        }
                    //    }
                    //}
                    //if (congsuatThietBi > 0)
                    //{
                    //    SanPhamDTO item = listPin.Where(p => p.CongSuat >= congsuatThietBi && p.LoaiSanPhamID==_PinType).ToList().OrderBy(p => p.CongSuat).FirstOrDefault();
                    //    if (item != null)
                    //        listPinResult.Add(item);
                    //}
                }*/
                #endregion
            }
            catch { }
            return listPinResult;
        }
        #endregion

        #region "[chọn danh sách các bình acquy phù hợp với công suất pin]"
        public List<SanPham_AcquySDK> GetListAcquyByPinPower(double _PinPower)
        {
            List<SanPham_AcquySDK> listResult = new List<SanPham_AcquySDK>();
            try
            {
                //tìm được hệ số ah từ công suất
                double TongHeSoAH = _PinPower / 12;

                ///B1: lấy bình acquy có cùng hệ số ah 
                ///
                SanPham_AcquySDK acQuyCungAH = null;
                try { acQuyCungAH = GetAllAcquy().Where(p => p.SoAH == TongHeSoAH).First(); }
                catch { }
                if (acQuyCungAH != null)
                {
                    listResult.Add(acQuyCungAH);
                    return listResult;
                }
                else
                {
                    ///B2: tìm bình acquy có hệ số ah lớn hơn.
                    ///
                    try
                    {
                        acQuyCungAH = GetAllAcquy().Where(p => p.SoAH > TongHeSoAH).First();
                    }
                    catch { }
                    ///B3: tìm danh sách các acquy có hệ số ah nhỏ hơn hệ số ah của tấm Pin
                    ///
                    List<SanPham_AcquySDK> listTemp = GetAllAcquy().Where(p => p.SoAH < TongHeSoAH).ToList();
                    if (listTemp.Count > 0)
                    {
                        int irun = 0;
                        while (irun < listTemp.Count)
                        {
                            if (TongHeSoAH > 0)
                            {
                                irun++;
                                foreach (SanPham_AcquySDK item in listTemp)
                                {
                                    double ahTemp = TongHeSoAH - item.SoAH;
                                    if (ahTemp >= 0)
                                    {
                                        TongHeSoAH -= item.SoAH;
                                        listResult.Add(item);
                                    }
                                }
                            }
                            else
                                break;
                        }
                    }
                    else
                    {
                        if (acQuyCungAH != null)
                        {
                            listResult.Add(acQuyCungAH);
                            return listResult;
                        }
                    }
                    if (listResult.Count > 0)
                    {
                        if (acQuyCungAH != null)
                        {
                            double moneyTotal = 0;
                            foreach (SanPham_AcquySDK item in listResult)
                            {
                                moneyTotal += item.Gia;
                            }
                            if (moneyTotal < acQuyCungAH.Gia)
                                return listResult;
                            else
                            {
                                listResult = new List<SanPham_AcquySDK>();
                                listResult.Add(acQuyCungAH);
                                return listResult;
                            }
                        }
                        else
                            return listResult;
                    }
                    #region "[rem here]"
                    ////lấy danh sách các bình acquy giảm dần theo CĐDĐ
                    //List<SanPham_AcquySDK> list = GetAllAcquy().OrderByDescending(p => p.SoAH).ToList();
                    //if (list.Count > 0)
                    //{
                    //    int irun = 0;
                    //    while (irun <list.Count)
                    //    {
                    //        irun++;
                    //        //danh sách các acquy có số ah lớn hơn tong ah
                    //        SanPham_AcquySDK AcquyMax = GetAllAcquy().Where(p => p.SoAH >= TongHeSoAH).ToList().OrderBy(p => p.SoAH).ToList().First();
                    //        if (AcquyMax != null)
                    //        {
                    //            TongHeSoAH -= AcquyMax.SoAH;
                    //            listResult.Add(AcquyMax);
                    //        }
                    //        else
                    //        {
                    //            SanPham_AcquySDK acquyMin = list.First();
                    //            if (acquyMin != null)
                    //            {
                    //                TongHeSoAH -= acquyMin.SoAH;
                    //                listResult.Add(acquyMin);
                    //            }
                    //        }
                    //    }    
                    //if (congSuatPin > 0)
                    //{
                    //    list = list.OrderBy(p => p.SoAH).ToList();
                    //    foreach (SanPham_AcquySDK item in list)
                    //    {
                    //        double temp = item.SoAH - congSuatPin;
                    //        if (temp > 0)
                    //        {
                    //            congSuatPin -= item.SoAH;
                    //            listResult.Add(item);
                    //        }
                    //    }   
                    //}

                }
                    #endregion
            }
            catch { }
            return listResult;
        }
        #endregion

        #region "[Lấy Danh sách các kích điện]"
        public List<SanPham_KichDienSDK> GetListChangeDCAC()
        {
            List<SanPham_KichDienSDK> listResult = new List<SanPham_KichDienSDK>();
            try
            {
                List<SanPhamDTO> list = GetAllPin();
                if (list.Count > 0)
                {
                    foreach (SanPhamDTO item in list)
                    {
                        if (GetType(NangLuongTieuDiem_Management.LoaiSanPham_SelectByPrimaryKey(item.LoaiSanPhamID).LoaiSanPhamChinhID) == ProductType.KichDien)
                        {
                            SanPham_KichDienSDK itemInsert = new SanPham_KichDienSDK();
                            itemInsert.ID = item.ID;
                            itemInsert.ThongSoKyThuat = item.HinhDang;
                            itemInsert.LoaiSanPhamID = item.LoaiSanPhamID;
                            itemInsert.NguonNhap = item.NguonNhap;
                            itemInsert.Gia = item.Gia;
                            itemInsert.Xoa = item.Xoa;
                            itemInsert.GhiChu = item.GhiChu;

                            listResult.Add(itemInsert);
                        }
                    }
                }
            }
            catch { }
            return listResult;
        }
        #endregion

        #region "[SelectByMulti]"// Cao Hoang An - 1241301
        //Lấy danh sách các đối tượng SanPham trên nhiêu tiêu chi
        public List<SanPhamDTO> SelectByMulti(bool flagNumberic, object Data, int LoaiSanPhamChinhID)
        {
            List<SanPhamDTO> dsspDto = new List<SanPhamDTO>();
            try
            {
                dsspDto = SanPhamDAO.SelectByMulti(flagNumberic,Data,LoaiSanPhamChinhID);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("SelectByMulti", ex);
            }
            return dsspDto;
        }
        #endregion

        #region "[Lấy danh sách các thiết bị điện]"
        /// <summary>
        /// Lấy danh sách các thiết bị điện
        /// </summary>
        /// <returns></returns>
        public List<SanPham_ThietBiDienSDK> GetAllElectrialEquipment()
        {
            List<SanPham_ThietBiDienSDK> listObjs = new List<SanPham_ThietBiDienSDK>();
            try {
                List<SanPhamDTO> list = GetAllPin().Where(p => p.Xoa == false).ToList();
                if (list.Count > 0)
                {
                    foreach (SanPhamDTO Item in list)
                    {
                        BUS.LoaiSanPhamBUS m_hendle = new LoaiSanPhamBUS();
                        if (GetType(m_hendle.SelectByPrimaryKey(Item.LoaiSanPhamID).LoaiSanPhamChinhID) == ProductType.ThietBiDien)
                        {
                            SanPham_ThietBiDienSDK ItemInsert = new SanPham_ThietBiDienSDK();
                            ItemInsert.ID = Item.ID;
                            ItemInsert.TenThietBi = Item.TenSanPham;
                            ItemInsert.ThongSoKyThuat = Item.HinhDang;
                            ItemInsert.Gia = Item.Gia;
                            ItemInsert.Xoa = Item.Xoa;
                            ItemInsert.GhiChu = Item.GhiChu;

                            listObjs.Add(ItemInsert);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("GetAllElectrialEquipment", ex);
            }
            return listObjs;
        }
        #endregion

        #region "[Lấy danh sách các bình acquy]"
        /// <summary>
        /// Lấy danh sách các bình acquy
        /// </summary>
        /// <returns></returns>
        public List<SanPham_AcquySDK> GetAllAcquy()
        {
            List<SanPham_AcquySDK> listObjs = new List<SanPham_AcquySDK>();
            try {
                List<SanPhamDTO> list = GetAllPin().Where(p => p.Xoa == false).ToList();
                if (list.Count > 0)
                {
                    foreach (SanPhamDTO Item in list)
                    { 
                        BUS.LoaiSanPhamBUS m_handle = new LoaiSanPhamBUS ();
                        if (GetType(m_handle.SelectByPrimaryKey(Item.LoaiSanPhamID).LoaiSanPhamChinhID) == ProductType.BinhAcQuy)
                        {
                            SanPham_AcquySDK objInsert = new SanPham_AcquySDK();
                            objInsert.ID = Item.ID;
                            objInsert.SoAH = Item.CongSuat;
                            objInsert.HieuDienThe = Item.Vmp;
                            objInsert.Gia = Item.Gia;
                            objInsert.NguonNhap = Item.NguonNhap;
                            objInsert.Xoa = false;
                            objInsert.GhiChu = Item.GhiChu;

                            listObjs.Add(objInsert);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("GetAllAcquy", ex);
            }
            return listObjs;
        }
        #endregion

        #region "[Lấy danh sách các bộ điều khiển sạc]"
        /// <summary>
        /// Lấy danh sách các bộ điều khiển sạc
        /// </summary>
        /// <returns></returns>
        public List<SanPham_BoDieuKhienSacSDK> GetAllChargeController()
        {
            List<SanPham_BoDieuKhienSacSDK> listObjs = new List<SanPham_BoDieuKhienSacSDK>();
            try
            {
                List<SanPhamDTO> list = GetAllPin().Where(p => p.Xoa == false).ToList();
                if (list.Count > 0)
                {
                    foreach (SanPhamDTO Item in list)
                    {
                        LoaiSanPhamBUS m_Handle = new LoaiSanPhamBUS();
                        if (GetType(m_Handle.SelectByPrimaryKey(Item.LoaiSanPhamID).LoaiSanPhamChinhID) == ProductType.BoDieuKienSac)
                        {
                            SanPham_BoDieuKhienSacSDK ItemInsert = new SanPham_BoDieuKhienSacSDK();
                            ItemInsert.ID = Item.ID;
                            ItemInsert.Ampe = Item.Imp;
                            ItemInsert.VMin = Item.Vmp;
                            ItemInsert.VMax = Item.Voc;
                            ItemInsert.Gia = Item.Gia;
                            ItemInsert.GhiChu = Item.GhiChu;

                            listObjs.Add(ItemInsert);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("GetAllChargeController", ex);
            }
            return listObjs;
        }
        #endregion

        #region "[SelectByLoaiSanPhamID]"// Cao Hoang An - 1241301
        //TimKiem san pham theo LoaiSanPhamID
        public List<SanPhamDTO> SelectByLoaiSanPhamID(int LoaiSanPhamID)
        {
            List<SanPhamDTO> dsspDto = new List<SanPhamDTO>();
            try
            {
                dsspDto = SanPhamDAO.SelectByLoaiSanPhamID(LoaiSanPhamID);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("SelectByLoaiSanPhamID", ex);
            }
            return dsspDto;
        }
        #endregion

        #region "[Lấy danh sách sản phẩm từ file excel]"
        /// <summary>
        /// Lấy danh sách sản phẩm từ file excel
        /// </summary>
        /// <param name="_strLocation">đường dẫn đến file excel</param>
        /// <param name="_iSheet">tên sheet vd: sheet 1 thì là: 1</param>
        /// <returns>danh sách các đối tượng ThietBiDienDTO</returns>
        public ACK.SanPhamACK GetListFromFileExcel(string _strLocation, int _iSheet)
        {
            ACK.SanPhamACK objResult = null;

            List<ThietBiDienDTO> listObj = new List<ThietBiDienDTO>();
            try
            {
                objResult = new ACK.SanPhamACK();

                string strPath = "provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + _strLocation + "';Extended Properties=Excel 8.0;";
                string strsql = "select * from [Sheet" + _iSheet.ToString() + "$]";
                System.Data.OleDb.OleDbConnection MyConnection;
                System.Data.DataSet DtSet;
                System.Data.OleDb.OleDbDataAdapter MyCommand;
                MyConnection = new System.Data.OleDb.OleDbConnection(strPath);
                MyCommand = new System.Data.OleDb.OleDbDataAdapter(strsql, MyConnection);
                MyCommand.TableMappings.Add("Table", "TestTable");
                DtSet = new System.Data.DataSet();
                MyCommand.Fill(DtSet);
                DataTable dt = DtSet.Tables[0];
                MyConnection.Close();
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        string NameType = row["TenLoaiSanPham"].ToString();
                        Regex regex = new Regex(@"\d+");
                        Match mc = regex.Match(NameType);
                        int iPOs = NameType.IndexOf("-");

                        string NameProductType =string.Empty;
                        if(iPOs>0)
                        {
                            NameProductType = NameType.Substring(iPOs+1);
                        }
                                                        LoaiSanPhamBUS m_Handle = new LoaiSanPhamBUS();
                        switch (GetType(NangLuongTieuDiem_Management.LoaiSanPham_SelectByPrimaryKey( int.Parse(mc.Value.Trim())).LoaiSanPhamChinhID))
                        {
                            case ProductType.Pin:

                                #region "[Thông tin Pin]"
                                SanPhamDTO sanPhamInsert = new SanPhamDTO();
                                sanPhamInsert.TenSanPham = row["TenSanPham"].ToString();
                                sanPhamInsert.LoaiSanPhamID = m_Handle.SelectByName(NameProductType).ID;
                                sanPhamInsert.CongSuat = double.Parse(row["CongSuat"].ToString());
                               
                                #region"[Lấy chi tiết thông tin kỹ thuật]"
                                string thongsokythuat = row["ThongSoKyThuat"].ToString();
                                int ipos1 = thongsokythuat.IndexOf("x");
                                if (ipos1 ==-1)
                                    ipos1 = thongsokythuat.IndexOf("*");
                                int ipos2 = thongsokythuat.LastIndexOf("x");
                                if (ipos2 == -1)
                                    ipos2 = thongsokythuat.LastIndexOf("*");
                                if (ipos1 >-1 && ipos2>-1)
                                {
                                    sanPhamInsert.Dai = double.Parse(thongsokythuat.Substring(0,ipos1));
                                    string k = thongsokythuat.Substring(ipos1 + 1, ((ipos2-1) - ipos1));
                                    sanPhamInsert.Rong = double.Parse(thongsokythuat.Substring(ipos1 + 1, ((ipos2 - 1) - ipos1)));
                                    sanPhamInsert.Day = double.Parse(thongsokythuat.Substring(ipos2 + 1));
                                }
                                #endregion

                                sanPhamInsert.Imp = double.Parse(row["Imp"].ToString());
                                sanPhamInsert.Vmp = double.Parse(row["Vmp"].ToString());
                                sanPhamInsert.Isc = double.Parse(row["Isc"].ToString());
                                sanPhamInsert.Voc = double.Parse(row["Voc"].ToString());
                                sanPhamInsert.NguonNhap = row["NguonNhap"].ToString();
                                sanPhamInsert.Gia =  double .Parse(row["Gia"].ToString());
                                sanPhamInsert.GhiChu = row["GhiChu"].ToString();

                                objResult.Pins.Add(sanPhamInsert);
                                #endregion

                                break;
                            case ProductType.BinhAcQuy:

                                #region "[Thông tin acquy]"
                                SanPham_AcquySDK acquyItem = new SanPham_AcquySDK();
                                acquyItem.TenSanPham = row["TenSanPham"].ToString();
                                acquyItem.LoaiSanPhamID = m_Handle.SelectByName(NameProductType).ID;
                                acquyItem.SoAH = double.Parse(row["SoAH"].ToString());
                                acquyItem.HieuDienThe = double.Parse(row["HieuDienThe"].ToString());
                                acquyItem.NguonNhap = row["NguonNhap"].ToString();
                                acquyItem.Gia = double.Parse(row["Gia"].ToString());
                                acquyItem.GhiChu = row["GhiChu"].ToString();

                                objResult.Acquys.Add(acquyItem);
                                #endregion

                                break;
                            case ProductType.BoDieuKienSac:

                                #region "[Thông tin bộ điều khiển sạc]"
                                SanPham_BoDieuKhienSacSDK dieuKhienSacItem = new SanPham_BoDieuKhienSacSDK();
                                dieuKhienSacItem.TenSanPham = row["TenSanPham"].ToString();
                                dieuKhienSacItem.LoaiSanPhamID =  m_Handle.SelectByName(NameProductType).ID;
                                dieuKhienSacItem.Ampe = double.Parse(row["Ampe"].ToString());
                                dieuKhienSacItem.VMax = double.Parse(row["HDTMax"].ToString());
                                dieuKhienSacItem.VMin = double.Parse(row["HDTMin"].ToString());
                                dieuKhienSacItem.NguonNhap = row["NguonNhap"].ToString();
                                dieuKhienSacItem.Gia = double.Parse(row["Gia"].ToString());
                                dieuKhienSacItem.GhiChu = row["GhiChu"].ToString();

                                objResult.BoDieuKhienSacs.Add(dieuKhienSacItem);
                                #endregion

                                break;
                            case ProductType.ThietBiDien:

                                #region "[Thông tin Thiết Bị Điện]"
                                SanPham_ThietBiDienSDK thietbidienItem = new SanPham_ThietBiDienSDK();
                                thietbidienItem.TenThietBi = row["TenSanPham"].ToString();
                                thietbidienItem.LoaiSanPhamID = m_Handle.SelectByName(NameProductType).ID;
                                thietbidienItem.ThongSoKyThuat = row["ThongSoKyThuat"].ToString();
                                thietbidienItem.ChatLieu = row["ChatLieu"].ToString();
                                thietbidienItem.NguonSang = row["NguonSang"].ToString();
                                thietbidienItem.TamThuNangLuong = row["TamThuNangLuong"].ToString();
                                thietbidienItem.GhiChu = row["UuDiem"].ToString();
                                thietbidienItem.NguonSang = row["NguonNhap"].ToString();
                                thietbidienItem.Gia = double.Parse(row["Gia"].ToString());

                                objResult.ThietBiDiens.Add(thietbidienItem);
                                #endregion

                                break;
                        }
                    }
                }
            }
            catch { }
            return objResult;
        }
        #endregion

        #region "[TimkiemPinNangLuong]"// tran ha nguyen thang - 1241301

        public List<SanPhamDTO> TimKiemTuVanPinNangLuong(double congsuat, int ID1, int ID2, int ID3)
        {
            List<SanPhamDTO> dsspDto = new List<SanPhamDTO>();
            try
            {
                dsspDto = SanPhamDAO.TimKiemTuVanPinNangLuong(congsuat, ID1, ID2, ID3);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("SelectByLoaiSanPhamID", ex);
            }
            return dsspDto;
        }
        #endregion

        #region "[TimkiemBoDieuKhienXac]"// tran ha nguyen thang - 1241301

        public List<SanPhamDTO> TimKiemTuVanBoDieuKhienXac(double Isc_max)
        {
            List<SanPhamDTO> dsspDto = new List<SanPhamDTO>();
            try
            {
                dsspDto = SanPhamDAO.TimKiemTuVanDieuKhienXac(Isc_max);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("SelectByLoaiSanPhamID", ex);
            }
            return dsspDto;
        }
        #endregion


        #region "[Lấy bộ điều khiển sạc đáp ứng công suất pin]"
        public SanPham_BoDieuKhienSacSDK GetChargeControllerByPinPower(double _PinPower)
        {
            SanPham_BoDieuKhienSacSDK objResult = null;
            try
            {
                List<SanPham_BoDieuKhienSacSDK> listdieukhiensac = GetAllChargeController().OrderBy(p => p.Ampe).ToList();
                if (listdieukhiensac.Count > 0)
                {
                    foreach (SanPham_BoDieuKhienSacSDK item in listdieukhiensac)
                    {
                        double congsuat = item.Ampe * (item.VMax - item.VMin);
                        if ((_PinPower <= congsuat))
                            return item;

                    }
                }
            }
            catch { }
            return objResult;
        }
        #endregion

        #region "[Hàm insert lỗi hệ thống]"//1241302 Đoàn Phú An
        public void InSert_LoiHeThong(string TenHam, Exception ex)
        {
            LoiHeThongDTO lhtDto = new LoiHeThongDTO();
            lhtDto.TenLoi = ex.Message.ToString();
            lhtDto.NgayTao = DateTime.Now;
            lhtDto.TenHamBiLoi = "SanPhamBUS_" + TenHam;
            lhtDto.SuaLoi = false;
            lhtDto.GhiChu = "";
            LoiHeThongDAO.Insert(lhtDto);
        }
        #endregion
    }
}
